
%macro pull_v(suf,a_where);
proc means data = inter.rep_insurance23 noprint;
   var   medicaid private self_pay other_ins;
   output out = &suf. (rename=(_FREQ_ = &suf.)) mean =  medi_&suf. priv_&suf. self_&suf. o_ins_&suf.;
   class months_23;
   where state in ('AZ','CA','IA','NJ','WI') &a_where.;
run;
%mend;

%pull_v(all,);
%pull_v(all_pub,and cntrl_public = 1);
%pull_v(all_non_prof,and cntrl_non_profit = 1);
%pull_v(all_for_prof,and cntrl_for_profit = 1);
%pull_v(all_other,and cntrl_other = 1);

%pull_v(all_np,and preg_related = 0);
%pull_v(all_np_pub,and preg_related = 0 and cntrl_public = 1);
%pull_v(all_np_non_prof,and preg_related = 0 and cntrl_non_profit = 1);
%pull_v(all_np_for_prof,and preg_related = 0 and cntrl_for_profit = 1);
%pull_v(all_np_other,and preg_related = 0 and cntrl_other = 1);

%pull_v(female,and female = 1);
%pull_v(female_pub,and female = 1 and cntrl_public = 1);
%pull_v(female_non_prof,and female = 1 and cntrl_non_profit = 1);
%pull_v(female_for_prof,and female = 1 and cntrl_for_profit = 1);
%pull_v(female_other,and female = 1 and cntrl_other = 1);

%pull_v(female_np,and preg_related = 0 and female = 1);
%pull_v(female_np_pub,and preg_related = 0 and female = 1 and cntrl_public = 1);
%pull_v(female_np_non_prof,and preg_related = 0 and female = 1 and cntrl_non_profit = 1);
%pull_v(female_np_for_prof,and preg_related = 0 and female = 1 and cntrl_for_profit = 1);
%pull_v(female_np_other,and preg_related = 0 and female = 1 and cntrl_other = 1);

%pull_v(female_p,and preg_related = 1 and female = 1);
%pull_v(female_p_pub,and preg_related = 1 and female = 1 and cntrl_public = 1);
%pull_v(female_p_non_prof,and preg_related = 1 and female = 1 and cntrl_non_profit = 1);
%pull_v(female_p_for_prof,and preg_related = 1 and female = 1 and cntrl_for_profit = 1);
%pull_v(female_p_other,and preg_related = 1 and female = 1 and cntrl_other = 1);

%pull_v(male,and female = 0);
%pull_v(male_pub,and female = 0 and cntrl_public = 1);
%pull_v(male_non_prof,and female = 0 and cntrl_non_profit = 1);
%pull_v(male_for_prof,and female = 0 and cntrl_for_profit = 1);
%pull_v(male_other,and female = 0 and cntrl_other = 1);

data counts (drop = _type_ );
   merge all 		all_pub 		all_non_prof 		all_for_prof 		all_other
    	 all_np 	all_np_pub 		all_np_non_prof 	all_np_for_prof 	all_np_other
         female 	female_pub 		female_non_prof 	female_for_prof 	female_other
		 female_np 	female_np_pub 	female_np_non_prof 	female_np_for_prof 	female_np_other
		 female_p 	female_p_pub 	female_p_non_prof 	female_p_for_prof 	female_p_other
		 male 		male_pub 		male_non_prof 		male_for_prof 		male_other;
   by months_23;
   if months_23 = . then delete;
   merge_v = 1;
run;


data inter.ins_23_rates (drop = 	merge_v 			i);
   merge counts inter.ins_23_pop_combined;
   by merge_v;
   age = 23+months_23/12;
   if months_23 = 0 then bmonth = 1; else bmonth = 0;
   if months_23 > 0 then post = 1; else post = 0;
   linear = months_23;
   linear_post = months_23*post;
   array _all{5}  	all 		all_pub 		all_non_prof 		all_for_prof 		all_other;
   array _all_r{5} 	all_r 		all_pub_r 		all_non_prof_r 		all_for_prof_r 		all_other_r;
   array _all_l{5} 	all_l 		all_pub_l 		all_non_prof_l 		all_for_prof_l 		all_other_l;
   array _all_np{5}  	all_np 		all_np_pub 		all_np_non_prof 		all_np_for_prof 		all_np_other;
   array _all_np_r{5} 	all_np_r 	all_np_pub_r 	all_np_non_prof_r 		all_np_for_prof_r 		all_np_other_r;
   array _all_np_l{5} 	all_np_l 	all_np_pub_l 	all_np_non_prof_l 		all_np_for_prof_l 		all_np_other_l;
   array _female{5}  	female 		female_pub 		female_non_prof 		female_for_prof 		female_other;
   array _female_r{5} 	female_r 	female_pub_r 	female_non_prof_r 		female_for_prof_r 		female_other_r;
   array _female_l{5} 	female_l 	female_pub_l 	female_non_prof_l 		female_for_prof_l 		female_other_l;
   array _female_np{5}  	female_np 		female_np_pub 		female_np_non_prof 		female_np_for_prof 		female_np_other;
   array _female_np_r{5} female_np_r 	female_np_pub_r 	female_np_non_prof_r 	female_np_for_prof_r 	female_np_other_r;
   array _female_np_l{5} female_np_l 	female_np_pub_l 	female_np_non_prof_l 	female_np_for_prof_l 	female_np_other_l;
   array _female_p{5}  	female_p 	female_p_pub 	female_p_non_prof 		female_p_for_prof 		female_p_other;
   array _female_p_r{5} female_p_r 	female_p_pub_r 	female_p_non_prof_r 	female_p_for_prof_r 	female_p_other_r;
   array _female_p_l{5} female_p_l 	female_p_pub_l 	female_p_non_prof_l 	female_p_for_prof_l 	female_p_other_l;
   array _male{5}  	male 		male_pub 		male_non_prof 		male_for_prof 		male_other;
   array _male_r{5} 	male_r 		male_pub_r 		male_non_prof_r 	male_for_prof_r 	male_other_r;
   array _male_l{5} 	male_l 		male_pub_l 		male_non_prof_l 	male_for_prof_l 	male_other_l;
   do i = 1 to 5;
      _all_r{i} = 10000*_all{i}/pop_all_AZ_CA_IA_NJ_WI;
      _all_np_r{i} = 10000*_all_np{i}/pop_all_AZ_CA_IA_NJ_WI;
      _male_r{i} = 10000*_male{i}/pop_male_AZ_CA_IA_NJ_WI ;
      _female_r{i} = 10000*_female{i}/pop_female_AZ_CA_IA_NJ_WI;
      _female_np_r{i} = 10000*_female_np{i}/pop_female_AZ_CA_IA_NJ_WI;
      _female_p_r{i} = 10000*_female_p{i}/pop_female_AZ_CA_IA_NJ_WI;
      _all_l{i} = log(_all{i});
      _all_np_l{i} = log(_all_np{i});
      _male_l{i} = log(_male{i});
      _female_l{i} = log(_female{i});
      _female_np_l{i} = log(_female_np{i});
      _female_p_l{i} = log(_female_p{i});
   end;
run;

proc means data = inter.ins_23_rates mean;
   var all_r all_np_r female_r female_np_r male_r;
   where months_23 >= -12 and months_23 <=12;
run;

PROC EXPORT DATA= inter.ins_23_rates
            OUTFILE= "C:\Research\Insurance 23\3. Code up from Scratch\P03 ED Analysis File.csv"
            DBMS=CSV REPLACE;
RUN;

%let keep_v1 = 
all 				medi_all      				priv_all     				self_all     			o_ins_all
all_pub 			medi_all_pub  				priv_all_pub 				self_all_pub 			o_ins_all_pub
all_non_prof 		medi_all_non_prof  			priv_all_non_prof 			self_all_non_prof 		o_ins_all_non_prof	
all_for_prof 		medi_all_for_prof  			priv_all_for_prof 			self_all_for_prof 		o_ins_all_for_prof	
all_other			medi_all_other  			priv_all_other 				self_all_other 			o_ins_all_other

all_np 				medi_all_np  				priv_all_np 				self_all_np 			o_ins_all_np
all_np_pub 			medi_all_np_pub  			priv_all_np_pub 			self_all_np_pub 		o_ins_all_np_pub
all_np_non_prof 	medi_all_np_non_prof  		priv_all_np_non_prof 		self_all_np_non_prof 	o_ins_all_np_non_prof	
all_np_for_prof 	medi_all_np_for_prof  		priv_all_np_for_prof 		self_all_np_for_prof 	o_ins_all_np_for_prof	
all_np_other  		medi_all_np_other   		priv_all_np_other  			self_all_np_other  		o_ins_all_np_other 

female 				medi_female  				priv_female			 		self_female 			o_ins_female
female_pub 			medi_female_pub  			priv_female_pub 			self_female_pub 		o_ins_female_pub	
female_non_prof 	medi_female_non_prof  		priv_female_non_prof 		self_female_non_prof 	o_ins_female_non_prof 	
female_for_prof 	medi_female_for_prof  		priv_female_for_prof 		self_female_for_prof 	o_ins_female_for_prof	
female_other 		medi_female_other  			priv_female_other 			self_female_other 		o_ins_female_other

female_np 			medi_female_np  			priv_female_np 				self_female_np  		o_ins_female_np
female_np_pub  		medi_female_np_pub  		priv_female_np_pub 			self_female_np_pub	 	o_ins_female_np_pub 	
female_np_non_prof  medi_female_np_non_prof  	priv_female_np_non_prof 	self_female_np_non_prof o_ins_female_np_non_prof 	
female_np_for_prof 	medi_female_np_for_prof  	priv_female_np_for_prof 	self_female_np_for_prof o_ins_female_np_for_prof
female_np_other  	medi_female_np_other 	 	priv_female_np_other 		self_female_np_other 	o_ins_female_np_other

female_p 			medi_female_p  			priv_female_p 			self_female_p  			o_ins_female_p
female_p_pub  		medi_female_p_pub  		priv_female_p_pub 		self_female_p_pub	 	o_ins_female_p_pub 	
female_p_non_prof   medi_female_p_non_prof  priv_female_p_non_prof 	self_female_p_non_prof 	o_ins_female_p_non_prof 	
female_p_for_prof 	medi_female_p_for_prof  priv_female_p_for_prof 	self_female_p_for_prof 	o_ins_female_p_for_prof
female_p_other  	medi_female_p_other 	priv_female_p_other 	self_female_p_other 	o_ins_female_p_other

male 				medi_male  					priv_male 					self_male 				o_ins_male
male_pub 			medi_male_pub  				priv_male_pub 				self_male_pub 			o_ins_male_pub
male_non_prof	 	medi_male_non_prof  		priv_male_non_prof 			self_male_non_prof 		o_ins_male_non_prof	
male_for_prof 		medi_male_for_prof  		priv_male_for_prof 			self_male_for_prof 		o_ins_male_for_prof	
male_other  		medi_male_other 			priv_male_other 			self_male_other 		o_ins_male_other; 

data for_mike_tal;
   set inter.ins_23_rates (keep = months_23 &keep_v1.);
   if months_23 >= -36 and months_23 <= 36;
run;

PROC EXPORT DATA= for_mike_tal
            OUTFILE= "C:\Research\Insurance 23\3. Code up from Scratch\P03 ED CSV File.csv"
            DBMS=CSV REPLACE;
RUN;


proc datasets;
   delete counts all male female all_AZ all_CA all_IA all_NJ all_WI;
run;

proc means data = inter.rep_insurance23 sum;
   var visit;
   where state in ('AZ','CA','IA','NJ','WI') and months_23 >= -12 and months_23 <= 11;
   title "Counts for all";
run;

proc means data = inter.rep_insurance23 sum;
   var visit;
   where female = 0 and state in ('AZ','CA','IA','NJ','WI') and months_23 >= -12 and months_23 <= 11;
   title "Counts for men";
run;

proc means data = inter.rep_insurance23 sum;
   var visit;
   where female = 1 and state in ('AZ','CA','IA','NJ','WI') and months_23 >= -12 and months_23 <= 11;
   title "Counts for women";
run;

proc means data = inter.rep_insurance23 sum;
   var visit;
   where  months_23 >= -12 and months_23 <= 11;
   title "Counts for all";
   class state;
run;
